package com.crm.controller.system;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.base.ResponseResult;
import com.crm.domain.dto.UserDto;
import com.crm.domain.entity.User;
import com.crm.domain.query.UserQuery;
import com.crm.domain.vo.UserVo;
import com.crm.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @author Hulunico
 */
@RestController
@Api(tags = "用户管理")
@RequestMapping("/system/user")
@RequiredArgsConstructor
public class UserController {
    private final IUserService userService;

    /**
     * 分页查询用户信息
     * @param userQuery
     * @return
     */
    @ApiOperation("用户列表")
    @GetMapping("/list")
    public ResponseResult list(UserQuery userQuery) {
        Page<UserVo> page =userService.queryAll(userQuery);
        return ResponseResult.success().put("rows",page.getRecords())
                                        .put("total",page.getTotal())
                                        .put("params",null);
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    @GetMapping("/{userId}")
    @ApiOperation("根据id查询用户")
    public ResponseResult getUserById(@PathVariable("userId") Long id){
        User user = userService.lambdaQuery().eq(User::getUserId,id).one();
        return ResponseResult.success(user);
    }

    // TODO 关联role表
    @GetMapping
    @ApiOperation("查询用户信息")
    public ResponseResult getUserInfo(){
        userService.getUserInfo();
        return ResponseResult.success();
    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    @PostMapping
    @ApiOperation("新增用户")
    public ResponseResult addUser(@RequestBody User user) {
        userService.save(user);
        return ResponseResult.success();
    }

    /**
     * 修改状态
     * @param user
     * @return
     */
    @PutMapping("/changeStatus")
    @ApiOperation("修改用户状态")
    public ResponseResult changeStatus(@RequestBody User user) {
        userService.lambdaUpdate().eq(User::getUserId,user.getUserId()).update(user);
        return ResponseResult.success();
    }

    /**
     * 根据Id批量删除
     * @param userIds
     * @return
     */
    @DeleteMapping("{userIds}")
    @ApiOperation("删除用户")
    public ResponseResult deleteUser(@PathVariable List<Long> userIds) {
        userIds.forEach(userId ->{
            userService.lambdaUpdate().eq(User::getUserId,userId).remove();
        });
        return ResponseResult.success();
    }

    @PutMapping
    @ApiOperation("修改用户信息")
    public ResponseResult resetPwd(@RequestBody UserDto userDto) {
        User user = BeanUtil.copyProperties(userDto, User.class);
        userService.updateById(user);
        return ResponseResult.success();
    }



    @GetMapping("/listselect")
    public ResponseResult listSelect(@RequestParam(value = "deptId")Long deptId){
        LambdaQueryChainWrapper<User> eq = userService.lambdaQuery().eq(User::getDeptId, deptId);
        return ResponseResult.success(eq.list());
    }


    @PutMapping("/resetPwd")
    @ApiOperation("修改用户密码")
    public ResponseResult resetPwd(@RequestBody User user) {
        userService.lambdaUpdate().eq(User::getPassword,user.getPassword()).update(user);
        return ResponseResult.success();
    }
    @PostMapping("/importData")
    @ApiOperation("导入用户")
    public ResponseResult importData(@RequestBody MultipartFile file) throws Exception {
        userService.importData(file);
        return ResponseResult.success();
    }

    // TODO: 路径没找到 跟上传文件的接口有关
    @GetMapping("/export")
    @ApiOperation("导出用户")
    public Map<String,Object> export() throws Exception {
        return userService.export();
    }

}
